//
//  RBResetPsdController.m
//  StoryToy
//
//  Created by baxiang on 2017/10/22.
//  Copyright © 2017年 roobo. All rights reserved.
//

#import "RBResetPsdController.h"
#import "RBLoginController.h"
#import "RTLineTextField.h"
#import <RBToyAPI/RBToyAPI.h>
#import "UIImage+RBExtension.h"

@interface RBResetPsdController ()<UITextFieldDelegate>
@property (nonatomic, weak) RTLineTextField * verifyTxtV;
@property (nonatomic, weak) RTLineTextField * psdTxtV;
@property (nonatomic, weak) UIButton * resetBtn;
@property (nonatomic, weak) UIButton *verifyBtn;

@end


@implementation RBResetPsdController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回密码";
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *verifyBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:verifyBtn];
    verifyBtn.layer.masksToBounds = YES;
    verifyBtn.layer.cornerRadius = 35 *0.5;
    [verifyBtn setBackgroundImage:[UIImage imageWithColor:RTColorForBackground] forState:UIControlStateNormal];
    [verifyBtn setBackgroundImage:[UIImage imageWithColor: UIColorHex(0xc0c0c1)] forState:UIControlStateDisabled];
    [verifyBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    verifyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [verifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [verifyBtn addTarget:self action :@selector(sendVerifyCode) forControlEvents:UIControlEventTouchUpInside];
   
    _verifyBtn = verifyBtn;
    
    RTLineTextField *verifyTxtV = [RTLineTextField new];
    verifyTxtV.placeholder =  @"4位短信验证码";
    verifyTxtV.keyboardType = UIKeyboardTypePhonePad;
    verifyTxtV.textColor = UIColorHex(576166);;
    verifyTxtV.font = [UIFont systemFontOfSize:13];
    _verifyTxtV = verifyTxtV;
    [self.view addSubview:verifyTxtV];
    [verifyTxtV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(90);
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(verifyBtn.mas_left).offset(-10);
        make.height.mas_equalTo(45);
    }];
    
    [verifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-50);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(35);
        make.bottom.mas_equalTo(verifyTxtV.mas_bottom);
    }];

    RTLineTextField *psdTxtV = [RTLineTextField new];
    psdTxtV.placeholder =  @"请输入6-20位密码";
    psdTxtV.returnKeyType = UIReturnKeyNext;
    psdTxtV.secureTextEntry = YES;
    psdTxtV.textColor = UIColorHex(576166);;
    psdTxtV.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:psdTxtV];
    [psdTxtV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(verifyTxtV.mas_left);
        make.right.mas_equalTo(-45);
        make.height.mas_equalTo(45);
        make.top.mas_equalTo(verifyTxtV.mas_bottom).offset(20);
    }];
    _psdTxtV = psdTxtV;
    UIButton *secBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [secBtn setImage:[UIImage imageNamed:@"icon_eyes_close"] forState:UIControlStateNormal];
    [secBtn setImage:[UIImage imageNamed:@"icon_eyes_open"] forState:UIControlStateSelected];
    secBtn.frame = CGRectMake(0, 0, 26, 13);
    psdTxtV.rightView = secBtn;
    psdTxtV.rightViewMode = UITextFieldViewModeAlways;
    @weakify(self);
    [[secBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * btn) {
        @strongify(self);
        [btn setSelected:!btn.isSelected];
        self.psdTxtV.secureTextEntry = !btn.isSelected;
    }];
    
    UIButton*resetBtn =[UIButton buttonWithType: UIButtonTypeCustom];
    [resetBtn setTitle:@"重置密码" forState:UIControlStateNormal];
    [resetBtn addTarget:self action:@selector(resetClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:resetBtn];
    resetBtn.layer.cornerRadius = 45*0.5;
    resetBtn.layer.masksToBounds = YES;
    [resetBtn setBackgroundImage:[UIImage imageWithColor:RTColorForBackground] forState:UIControlStateNormal];
//    [resetBtn setBackgroundImage:[UIImage imageWithColor:UIColorHex(c0c0c1)] forState:UIControlStateDisabled];
    [resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(psdTxtV.mas_left);
        make.right.mas_equalTo(psdTxtV.mas_right);
        make.top.mas_equalTo(psdTxtV.mas_bottom).offset(45);
        make.height.mas_equalTo(45);
    }];
    _resetBtn = resetBtn;
    [self rac_liftSelector:@selector(checkCode:password:) withSignalsFromArray:@[self.verifyTxtV.rac_textSignal, self.psdTxtV.rac_textSignal]];
    [self sendVerifyCode];
}

- (void)checkCode:(NSString *)phone password:(NSString *)password {
    if (phone.length > 0 && password.length > 0) {
        self.resetBtn.enabled = YES;

    }else {
        self.resetBtn.enabled = NO;

    }
    
}

-(void)sendVerifyCode
{
    self.verifyBtn.enabled = NO;
    [RBUserApi sendCode:_phoneNum type:RBSendCodeTypeResetPsd completionBlock:^(BOOL isSend, NSError * _Nonnull error) {
        if (isSend) {
//            [RTUserMgr showWithText:@"验证码已发送" inView:self.view];
//           [self.verifyBtn startCountDownWithSecond:60];
        }else{
             self.verifyBtn.enabled = YES;
//            [RTUserMgr showWithError:error inView:self.view];
            
        }
    }];
}


- (void)resetClick{
    @weakify(self);
    [RBUserApi  resetPassword:_psdTxtV.text phoneNum:_phoneNum code:_verifyTxtV.text completionBlock:^(BOOL isSuccess, NSError * _Nonnull error) {
        @strongify(self);
        if (isSuccess) {
            NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
            for (UIViewController *aViewController in allViewControllers) {
                if ([aViewController isKindOfClass:[RBLoginController class]]) {
                    [self.navigationController popToViewController:aViewController animated:NO];
                }
            }
        }else{
//           [RTUserMgr showWithError:error inView:self.view];
        }
    }];
}

@end
